﻿
using hentity;
using hutil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 namespace hcache {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：TransferChain                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/10/10 9:12:53                        
   *└────────────────────────────────────────────────┘
   **/
    public class Estimate:CacheData  {

        private static long IdIndex = 1;
        public static long Id () {
           
                return IdIndex++;
            

        }
        public long id;
        public long beginOccupy;
        public DateTime beginDate;
        public DateTime finshDate;
        public long finshOccupy;//预计完成
        public int conmonBeforeTime;
        public int conmonNextTime;
        public int currentTime;
        public int nodeId  ;
        public long beforeEstimateId;
        public long nextEstimateId;
        public long myOrder;
        public int taskNo;
        public Estimate(BaseTask t, Node node) {
            this.id = Id();
            this.myOrder = t.myOrder;
            MyCache.addOver(this);
         
            this.nodeId = node.eid;
            taskNo = t.taskNo;
            node.timeAxis.Add(id);
        }
        public void addBefore(Estimate before) {
            if( before != null ) {
                before.nextEstimateId = this.id;
                this.beforeEstimateId = before.id;
            }
           
        }
       
        public int getWaitTime() {
            long wait =  finshOccupy - beginOccupy - conmonBeforeTime - conmonNextTime - currentTime;
            return Math.Max( (int)wait,0);
        }
        public void arrived() {//下位机通知上位机当前位置

            removeBefore();
            fillOccupy(DateTime.Now.Ticks / 10000);
            arrived(MyCache.getValue<Estimate>(""+nextEstimateId) );
        }

        private void removeBefore() {

            Estimate before = MyCache.getValue<Estimate>(""+beforeEstimateId);
            while( before != null ) {
                MyCache.remove<Estimate>(""+before.id);
                before = MyCache.getValue<Estimate>("" + before.beforeEstimateId);
            }
        }
        private void arrived(Estimate me) {
            if( me == null ) {
                return;
            }
            Estimate b = MyCache.getValue<Estimate>("" + me.beforeEstimateId);
            me.fillOccupy(b.finshOccupy - me.conmonBeforeTime);
            arrived(MyCache.getValue<Estimate>("" + me.nextEstimateId));
        }
        private void fillOccupy(long begin) {
            beginOccupy = begin;
            beginDate = new DateTime(begin * 10000);
            Estimate next = MyCache.getValue<Estimate>("" + nextEstimateId);
            if( null != next ) {
                long bestFinshOccupy = beginOccupy + conmonBeforeTime + currentTime;
                Node nextNode = TransferConfigFacade.getConfig(next.nodeId);
                finshOccupy = nextNode.getFreeTime(bestFinshOccupy ,next) + conmonNextTime;
            } else {
                finshOccupy = begin;
            }
            finshDate = new DateTime(finshOccupy * 10000);
        }

        public string getId () {
          return id+"";
        }
    }
}
